﻿using System;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Threading;

namespace Gao.Poker.App.UserControls
{
    /// <summary>
    /// Interaction logic for TimerControl.xaml
    /// </summary>
    public partial class TimerControl : UserControl
    {
        private readonly DispatcherTimer timer;

        private ProcessCount processCount;

        private int _minute;

        public int Minute
        {
            get { return _minute; }
            set 
            {
                _minute = value; 
            }
        }
        public int TimeFontSize { get; set; }
        public Brush BackColor { get; set; }

        public Brush TextColor { get; set; }
        /// <summary>
        /// 倒计时
        /// </summary>
        public Boolean Reduce { get; set; }

        public TimerControl()
        {
            InitializeComponent();
            DataContext = this;

            //设置定时器
            timer = new DispatcherTimer();
            timer.Interval = new TimeSpan(10000000); //时间间隔为一秒
            timer.Tick += new EventHandler(timer_Tick);
        }

        public void Pause()
        {
            timer.Stop();
        }

        public void Continue()
        {
            timer.Start();
        }

        public void Refresh()
        {
            Init();

            if (timer.IsEnabled) timer.Stop();
            //开启定时器
            timer.Start();
        }

        /// <summary>
        /// 初始化
        /// </summary>
        public void Init()
        {
            //处理倒计时的类
            processCount = new ProcessCount(Minute * 60);
            if (Reduce)
            {
                CountTime -= new CountHandler(processCount.ProcessCountDown);
                processCount.CurrentSecond = Minute * 60;
                CountTime += new CountHandler(processCount.ProcessCountDown);
            }
            else
            {
                processCount.TotalSecond = Minute * 60;
                processCount.CurrentSecond = 0;
                CountTime -= new CountHandler(processCount.ProcessCountUp);
                CountTime += new CountHandler(processCount.ProcessCountUp);
            }

        }


        /// <summary>
        /// Timer触发的事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void timer_Tick(object sender, EventArgs e)
        {
            if (OnCountDown())
            {
                MinuteArea.Text = processCount.GetMinute();
                SecondArea.Text = processCount.GetSecond();
            }
            else
            {
                timer.Stop();
                if (TimeClear != null)
                    TimeClear();
            }
                
        }

        /// <summary>
        /// 处理事件
        /// </summary>
        public event CountHandler CountTime;

        public event TimeClearHandler TimeClear;

        public bool OnCountDown()
        {
            if (CountTime != null)
                return CountTime();

            return false;
        }
    }

    /// <summary>
    /// 处理计时的委托
    /// </summary>
    /// <returns></returns>
    public delegate bool CountHandler();

    /// <summary>
    /// 计数器计数结束的委托
    /// </summary>
    public delegate void TimeClearHandler();

    /// <summary>
    /// 实现倒计时功能的类
    /// </summary>
    public class ProcessCount
    {


        private Int32 _currentSecond;

        public Int32 TotalSecond { get; set; }

        public Int32 CurrentSecond
        {
            get { return _currentSecond; }
            set { _currentSecond = value; }
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        public ProcessCount(Int32 totalSecond)
        {
            TotalSecond = totalSecond;
        }

        /// <summary>
        /// 减秒
        /// </summary>
        /// <returns></returns>
        public bool ProcessCountDown()
        {
            if (_currentSecond == 0)
                return false;
            else
            {
                _currentSecond--;
                return true;
            }
        }

        /// <summary>
        /// 加秒
        /// </summary>
        /// <returns></returns>
        public bool ProcessCountUp()
        {
            if (_currentSecond >= TotalSecond)
            {
                return false;
            }
            else
            {
                _currentSecond++;
                return true;
            }
        }

        ///// <summary>
        ///// 获取小时显示值
        ///// </summary>
        ///// <returns></returns>
        //public string GetHour()
        //{
        //    return String.Format("{0:D2}", (_currentSecond / 3600));
        //}

        /// <summary>
        /// 获取分钟显示值
        /// </summary>
        /// <returns></returns>
        public string GetMinute()
        {
            return String.Format("{0:D2}", (_currentSecond) / 60);
        }

        /// <summary>
        /// 获取秒显示值
        /// </summary>
        /// <returns></returns>
        public string GetSecond()
        {
            return String.Format("{0:D2}", _currentSecond % 60);
        }

    }
}